<?php
include "../header.inc";
require_once("../DataBase.inc");
require_once("activities.inc");

if (isset($_GET["remove_user"])) {
	DataBase::delete("staff",array("username"=>$_GET["remove_user"]));
	DataBase::delete("staff_roles",array("username"=>$_GET["remove_user"]));
	DataBase::delete("supervisor",array("supervisor_username"=>$_GET["remove_user"]));
	DataBase::delete("interviewer",array("interviewer_username"=>$_GET["remove_user"]));
	DataBase::delete("interview_observer",array("observer_username"=>$_GET["remove_user"]));
}

?>
<h1>Staff list</h1>
<button onclick="location.href='edit_staff.php';return false">Add new staff</button>
<table border=1 style='border-collapse:collapse'>
<tr><th></th><th>Username</th><th>First Name</th><th>Last Name</th><th>Roles</th><th>Next activities</th><th>Activities done before</th></tr>
<?php
$list = DataBase::select("staff",null,null,"username");
foreach ($list as $staff) {
	$roles = DataBase::select("staff_roles",null,array("username"=>$staff["username"]));
	echo "<tr>";
	echo "<td nowrap='nowrap'>";
	echo "<a href='edit_staff.php?username=".$staff["username"]."'><img src='".$_SESSION["root_url"]."/images/edit.gif' hspace=2 vspace=0 border=0/></a>";
	echo "<a href='?remove_user=".$staff["username"]."' onclick=\"return confirm('Are you sure you want to remove user ".$staff["username"]." ?');\"><img src='".$_SESSION["root_url"]."/images/trash.gif' hspace=2 vspace=0 border=0/></a>";
	echo "</td>";
	echo "<td nowrap='nowrap'>".$staff["username"]."</td>";
	echo "<td nowrap='nowrap'>".$staff["first_name"]."</td>";
	echo "<td nowrap='nowrap'>".$staff["last_name"]."</td>";
	echo "<td nowrap='nowrap'>";
	if ($roles <> false)
		for ($i = 0; $i < count($roles); $i++) {
			if ($i > 0) echo ", ";
			echo $roles[$i]["role"];
		}
	echo "</td>";
	$activities = get_staff_activities($staff["username"]);
	$before = array();
	$after = array();
	$now = time();
	foreach ($activities as $a) {
		$time = strtotime($a["DATE"]." ".$a["END_TIME"]);
		if ($time < $now)
			array_push($before, $a);
		else
			array_push($after, $a);
	}
	echo "<td nowrap='nowrap'><ul style='margin:0px'>";
	$list = get_staff_activities_description($after);
	foreach ($list as $a) echo "<li>".$a."</li>";
	echo "</ul></td>";
	echo "<td nowrap='nowrap'><ul style='margin:0px'>";
	$list = get_staff_activities_description($before);
	foreach ($list as $a) echo "<li>".$a."</li>";
	echo "</ul></td>";
	echo "</tr>";
}
?>
</table>
<?php include "../footer.inc";?>
